﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class RushMazeTraderPage : Page
    {
        public UILabel mazeFloorLabel;
        public CommonIcon itemIcon;
        public UILabel itemNameLabel;
        public UILabel originalPriceLabel;
        public UILabel discountPriceLabel;

        public GameObject buyBtn;
        public GameObject buyedTip;

        protected override void DoOpen()
        {
            ProtoBuf.Message.CSRushMazeTrader msg = new ProtoBuf.Message.CSRushMazeTrader();
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRushMazeTrader, msg, (pack, b) => {
                if (b)
                {
                    ProtoBuf.Message.SCRushMazeTrader _msg = pack.kBody as ProtoBuf.Message.SCRushMazeTrader;
                    Refresh(_msg);
                }
            });
        }

        void Refresh(ProtoBuf.Message.SCRushMazeTrader msg)
        {
            mazeFloorLabel.text = string.Format(TableTextBlo.GetText("di_x_ceng_maze_trader"), StaticData.rushCurFloorNum);
            itemIcon.SetData(msg.result.id, true, msg.result.num);
            itemNameLabel.text = TableTextBlo.GetText("name_" + msg.result.id);
            originalPriceLabel.text = msg.originalPrice.ToString();
            discountPriceLabel.text = msg.discountPrice.ToString();

            if (StaticData.isMazeTraderValid)
            {
                buyBtn.SetActive(true);
                buyedTip.SetActive(false);
            }
            else
            {
                buyBtn.SetActive(false);
                buyedTip.SetActive(true);
            }
        }

        public void OnBuyButtonClick()
        {
            ProtoBuf.Message.CSRushMazeTraderBuy msg = new ProtoBuf.Message.CSRushMazeTraderBuy();
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRushMazeTraderBuy, msg, (pack, b) => {
                if (b)
                {
                    ProtoBuf.Message.SCRushMazeTraderBuy _msg = pack.kBody as ProtoBuf.Message.SCRushMazeTraderBuy;
                    StaticData.playerData.Delete(_msg.cost);
                    StaticData.playerData.AddResultObj(_msg.gain);
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("buy_success"));
                    Close();
                }
            });
        }
    }
}
